package com.easymin.weixin.manager.impl;

import com.easymin.weixin.common.DataForm;
import com.easymin.weixin.common.DataGrid;
import com.easymin.weixin.entity.Application;
import com.easymin.weixin.manager.ApplicationManager;
import com.easymin.weixin.service.ApplicationService;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;

@Service
public class DefaultApplicationManager implements ApplicationManager {

  @Autowired
  private ApplicationService applicationService;

  @Override
  public DataGrid<Application> findAll(int page, int rows) {

    int offset = (page - 1) * rows;

    RowBounds rowBounds = new RowBounds(offset, rows);

    List<Application> applications = applicationService.findAll(rowBounds);

    long total = applicationService.count();

    DataGrid<Application> dataGrid = new DataGrid<>();
    dataGrid.setRows(applications);
    dataGrid.setTotal(total);

    return dataGrid;

  }

  @Override
  public DataForm<Application> insert(Application application) {

    DataForm<Application> dataForm = new DataForm<>();

    boolean exists = applicationService.exists(application.getName());

    if (!exists) {

      application.setAppId(StringUtils.lowerCase(UUID.randomUUID().toString().replace("-", "")));
      application.setAppKey(StringUtils.lowerCase(UUID.randomUUID().toString().replace("-", "")));

      applicationService.insert(application);

      dataForm.setStatus(0);
      dataForm.setMessage("成功");
    } else {
      dataForm.setStatus(-1);
      dataForm.setMessage("应用已经存在");
    }

    return dataForm;
  }

  @Override
  public Application fetch(Long id) {

    return applicationService.fetch(id);
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public DataForm<Application> updateByPrimaryKey(Application application) {

    Application local = applicationService.fetch(application.getId());

    DataForm<Application> dataForm = new DataForm<>();

    if (null != local) {
      applicationService.update(application);
      dataForm.setStatus(0);
      dataForm.setMessage("成功");
      dataForm.setData(application);
    } else {
      dataForm.setStatus(-1);
      dataForm.setMessage("应用不存在");
    }

    return dataForm;
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public DataForm<Application> deleteByPrimaryKey(Long id) {

    applicationService.delete(id);

    DataForm<Application> dataForm = new DataForm<>();
    dataForm.setStatus(0);
    dataForm.setMessage("成功");

    return dataForm;
  }

  @Override
  public DataForm<Application> updateAppkey(Long id) {

    DataForm<Application> dataForm = new DataForm<>();

    Application application = applicationService.fetch(id);

    if (null != application) {
      application.setAppKey(StringUtils.lowerCase(UUID.randomUUID().toString().replace("-", "")));
      dataForm.setStatus(0);
      dataForm.setMessage("成功");
      dataForm.setData(application);
      applicationService.update(application);
    } else {
      dataForm.setStatus(-1);
      dataForm.setMessage("应用不存在");
    }


    return dataForm;
  }

  @Override
  public Application fetchByAppId(String appId) {


    return applicationService.fetchByAppId(appId);
  }
}
